#include "Color.h"
#include <sstream>

const int Color::ROJO = 0xFF0000;
const int Color::VERDE = 0x00FF00;
const int Color::AZUL = 0x0000FF;
const int Color::BLANCO = 0xFFFFFF;
const int Color::CIAN = 0x00FFFF;
const int Color::MAGENTA = 0xFF00FF;
const int Color::AMARILLO = 0xFFFF00;
const int Color::NEGRO = 0x000000;
const int Color::GRIS = 0x808080;
const int Color::ROSA = 0xFF8080;
const int Color::VIOLETA = 0x800080;
const int Color::CELESTE = 0x80FFFF;
const int Color::MARRON = 0x800000;
const int Color::NARANJA = 0xFF8000;
const int Color::LAVANDA = 0x8000FF;
const int Color::TURQUESA = 0x00FF80;
const int Color::OROVIEJO = 0xBBBB20;


Color::Color(){
	this->color = Color::VERDE;
}

Color::Color(const Color& color){
	this->color = color.color;
}

std::string Color::colorToString(const int& nro){
	std::stringstream sX;
	sX <<"hex"<<nro ;
	std::string* salida;
	salida = new std::string();
	if(sX.str().length() == 1){
		salida->append("0");
	}
	salida->append(sX.str());
	return (*salida);
}

int Color::formarColor(const int & r, const int & g, const int & b) { 
	return (r << 16) + (g << 8) + b; 
} 

int Color::getR(const int color) {
	return (color >> 16) & 0xFF;
}

int Color::getG(const int color) {
	return (color >> 8) & 0xFF;
}

int Color::getB(const int color) {
	return (color) & 0xFF;
}

int Color::stringToInt(std::string s){
	std::istringstream iss(s);
	int i;
	iss>>i;
	return i;
}

// PASE DE STRING HEXA A INT
char Color::xtod(char c){
	if (c>='0' && c<='9') return c-'0';
	if (c>='A' && c<='F') return c-'A'+10;
	if (c>='a' && c<='f') return c-'a'+10;
	return c=0;        // not Hex digit
}

int Color::HextoDec(const char *hex, int l){
	if (*hex==0) return(l);
	return HextoDec(hex+1, l*16+xtod(*hex)); // hex+1?
}
// FIN - PASE DE STRING HEXA A INT



Color::Color(const std::string& alpha){
	// TODO - Validar el formato #rrggbb
	std::string* tmp = new std::string(alpha);
	// Quito el numeral
	tmp->replace(0,1,STRING_EMPTY);
	this->color = HextoDec(tmp->c_str(),6);
}

Color::Color(const int& R,const int& G,const int& B){
	this->color = this->formarColor(R,G,B);
}

Color::Color(const Uint32& RGB){
	this->color = RGB;
}

Color* Color::clonar(){
	return new Color(this->getR(),this->getG(), this->getB());
}

std::string Color::getAlpha	(){
	std::stringstream ss;
	ss << "#" << colorToString(this->getR()) << colorToString(this->getG()) << colorToString(this->getB());
	return ss.str();
}

int Color::getR(){
	return this->getR(getColor());
}

int Color::getG(){
	return this->getG(getColor());
}

int Color::getB(){
	return this->getB(getColor());
}

int Color::getColor(){
	return this->color;
}

Color* Color::opuesto(){
	int r = (this->getR() + 255) % 256 ;
	int g = (this->getG() + 255) % 256 ;
	int b = (this->getB() + 255) % 256 ;

	return new Color(r,g,b);
}

Color::~Color(){
}
